﻿using CommonUtils;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Test.Core
{
    internal class 数据修复
    {
        public static void run()
        {
            var folders = new List<DirectoryInfo>();
            var folder = FolderUtil.Get("D:\\Subversion\\ygcg-dev\\jeecg-boot\\jeecg-boot\\jeecg-boot-module-system-ygcg" +
                "\\src\\main\\java\\org\\jeecg\\modules");
            folders.AddRange(folder.GetFolders());
            folder = FolderUtil.Get("D:\\Subversion\\ygcg-dev\\jeecg-boot\\jeecg-boot\\jeecg-boot-module-system" +
                "\\src\\main\\java\\org\\jeecg\\modules");
            folders.AddRange(folder.GetFolders());
            var list = new List<string>();
            foreach (var subFolder in folders)
            {
                var entityFolder = subFolder.GetChildFolder("entity");
                if (!entityFolder.Exists)
                    continue;
                foreach (var entity in entityFolder.GetFiles())
                {
                    if (entity.GetText().Contains("private String declareNum;"))
                    {
                        var table = StringUtil.CamelToUnderLine(entity.Name.Replace(".java", ""));
                        list.Add(table);
                    }
                }
            }
            list.Sort();
            var sb1 = new StringBuilder();
            var sb2 = new StringBuilder();
            foreach (var table in list)
            {
                sb1.AppendLine("ALTER TABLE /*{}*/ \t\"{}\" \tMODIFY \"declare_num\" VARCHAR(50);", StringUtil.BlankToCamel(table), table);
                sb2.AppendLine("update /*{}*/ \t{} t1 \tinner join cgc_declare_list t2 on t1.declare_num = convert(varchar, t2.num) set t1.declare_num = t2.id;", StringUtil.BlankToCamel(table), table);
            }
            sb1.Append(sb2).Print();
        }
    }
}
